package org.lgiavedoni.android.franq.command.general;

import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.lgiavedoni.android.franq.core.command.Command;
import org.lgiavedoni.android.franq.core.contact.Contact;
import org.lgiavedoni.android.franq.service.communication.CommunicationService;
import org.lgiavedoni.android.franq.service.phone.ContactService;
import org.lgiavedoni.android.franq.util.collection.CollectionUtils;

public class ContactsCommand extends Command {

	private static final String OPTION_LIST = "list";
	private static final String OPTION_COUNT = "count";

	@Override
	public void setOptions(Options options) {
		options.addOption(OPTION_LIST, false, "List all contacts");
		options.addOption(OPTION_COUNT, false, "Number of contacts");
	}

	@Override
	public boolean execute(CommandLine cmd) {
		if (cmd.hasOption(OPTION_LIST)) {
			List<Contact> contacts = ContactService.inst.getContacts();
			CommunicationService.inst.sendDefaultTextMessage("Contacts: \n" + CollectionUtils.toString(contacts, " - "));
			return true;
		}
		if (cmd.hasOption(OPTION_COUNT)) {
			CommunicationService.inst.sendDefaultTextMessage("You have " + ContactService.inst.getContacts().size() + " contacts!");
			return true;
		}
		return false;
	}

	@Override
	public String getCommandText() {
		return "contacts";
	}
}